package com.example.teesystem.common.component;
import com.example.teesystem.entity.Class;
import com.example.teesystem.entity.RadarStatistic;
import com.example.teesystem.mapper.*;
import com.example.teesystem.service.ExamPaperService;
import com.example.teesystem.service.IssueRecordService;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author pxr
 * @data 15/9/2022 下午6:25
 */
@Component
@RequiredArgsConstructor
public class AutoUpdate {

    private final ExamPaperService examPaperService;
    private final CourseMapper courseMapper;
    private final ClassMapper classMapper;
    private final ClassStudentMapper classStudentMapper;
    private final RadarStatisticMapper radarStatisticMapper;
    private final BoatMapper boatMapper;
    private final IssueMapper issueMapper;

    private final IssueRecordService issueRecordService;

    public static final String [] issueType = new String[]{"记忆型","理解型","应用型","逻辑型","概念型","综合型"};

    @Scheduled(fixedRate = 30*60*1000)
    void updateExamPaper(){
        examPaperService.updateExamPaper();
    }

    @Scheduled(fixedRate = 300*1000)
    void updateBoatGame(){
        boatMapper.updateBoatDelete(new Date());
    }

//    @Scheduled(fixedRate = 86400*1000)
//    void deleteBoatGame(){
//        boatMapper.delete();
//    }

    @Scheduled(cron = "0 0 3 * * ?")
//    @PostConstruct
    void staticStudentRadar(){
        List<Class> classList = classMapper.getClasses();
        for(Class myClass : classList){
            List<Integer> studentIds = classStudentMapper.getStudentIdByClassId(myClass.getId());
            for(Integer studentId : studentIds){
                Map<String,Double> map = issueRecordService.getStudentRadar(myClass.getCourseId(),studentId);
                RadarStatistic radarStatistic = new RadarStatistic();
                radarStatistic.setUid(studentId);
                radarStatistic.setCourseId(myClass.getCourseId());
                radarStatistic.setUpdateTime(new Date());
                radarStatistic.setMemory(map.get("记忆型"));
                radarStatistic.setUnderstand(map.get("理解型"));
                radarStatistic.setApplication(map.get("应用型"));
                radarStatistic.setLogic(map.get("逻辑型"));
                radarStatistic.setConcept(map.get("概念型"));
                radarStatistic.setComprehensive(map.get("综合型"));
                radarStatisticMapper.insert(radarStatistic);
            }
        }

    }
//    @PostConstruct
//    @Transactional
//    void updateIssue(){
//        String [] s = new String[]{"记忆型","理解型","应用型","逻辑型","概念型","综合型"};
//        List<Integer> integerList = issueMapper.get();
//        for (Integer integer : integerList) {
//            issueMapper.update(s[(int) (Math.random() * 6)], integer);
//        }
//    }

//    @PostConstruct
//    @Transactional
//    void updateDifficult(){
//        List<Issue> issues = issueMapper.getall();
//        for(Issue issue:issues){
//            issueMapper.updateDifficulty((int) (Math.random() * 5 + 1),issue.getId());
//        }
//
//    }
}
